<?php
/**
 * Celebrio Core
 * 
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    Celebrio
 * @subpackage Core
 */
namespace Celebrio\Core;

use Nette\Object;
use Albireo\Mode;

use Celebrio\Core\Manifest;
use Celebrio\Core\CriticalException;
use Celebrio\Core\ServiceProvider;

/**
 * Description of ModuleLoader
 *
 * @author pavel
 */
class ModuleLoader extends Object {
    const DEPENDENCIES = "dependencies";

    public function loadInstalled() {
        
        $manifestLoader = new ManifestLoader();
        $manifests = $manifestLoader->getAllManifests();
        
        try {    
            foreach ($manifests as $manifest) {
                if ($manifest->isInstalled()) {
                    $this->init($manifest); // may throw CriticalException
                    ServiceProvider::registerService($manifest);
                } else {
                    // not installed yet
                }
            }
            
            CriticalFeatures::check();    
            
        } catch (CriticalException $e) {
            Mode::setCriticalMode($e->getMessage());
        }
//        $service = ServiceProvider::getAllServices();
//        \Nette\Debug::dump($service);
    }
    
    private function init(Manifest $manifest) {
        $configuration = Config::current()->loadConfigArray($manifest->getFullNameDots());
        $initClass = $manifest->getInit();
        
        if ($initClass !== null) {
            $initClass->init($configuration);
        } else {
            //\Nette\Debug::dump("No initializing routine found for " . $manifest->getFullNameDots());
        }
    }

}

